package cn.kgc.mybatis.mapper;

import java.util.ArrayList;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/2/8
 * @Description:  jdbc中如何完成动态sql
 */
public class TestMapper {

    public static void main(String[] args) {

        /**
         * 搜索用户信息  username  state
         * 1.没有参数  select *  from user
         * 2.username  select *  from user where username = #{username}
         * 3.state  select *  from user where state = #{state}
         * 4.username  state  select *  from user where state = #{state}  and username = #{username}
         */

        // 根据传递参数的不同 执行不同的sql

        String username = null;
        Integer state = 1;

        String sql = "select *  from user where 1==1 ";

        ArrayList<Object> params = new ArrayList<>();

        if(username!=null){
            sql+="  and username = ?";
            params.add(username);
        }

        if(state!=null){
            sql+= " and state = ?";
            params.add(state);
        }

        System.out.println("params = " + params);
        System.out.println("sql = " + sql);

    }

}
